package guda.shop.cms.manager.impl;

import guda.shop.cms.dao.ProductTagDao;
import guda.shop.cms.entity.ProductTag;
import guda.shop.cms.manager.ProductMng;
import guda.shop.cms.manager.ProductTagMng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Transactional
public class ProductTagMngImpl
        implements ProductTagMng {
    private ProductMng productMng;
    private ProductTagDao productTagDao;

    @Transactional(readOnly = true)
    public List<ProductTag> getList(Long paramLong) {
        List localList = this.productTagDao.getList(paramLong);
        return localList;
    }

    @Transactional(readOnly = true)
    public ProductTag findById(Long paramLong) {
        ProductTag localProductTag = this.productTagDao.findById(paramLong);
        return localProductTag;
    }

    public ProductTag save(ProductTag paramProductTag) {
        paramProductTag.init();
        this.productTagDao.save(paramProductTag);
        return paramProductTag;
    }

    public ProductTag[] updateTagName(Long[] paramArrayOfLong, String[] paramArrayOfString) {
        Assert.notEmpty(paramArrayOfLong);
        Assert.notEmpty(paramArrayOfString);
        if (paramArrayOfLong.length != paramArrayOfString.length)
            throw new IllegalArgumentException("ids length not equals tagNames length");
        ProductTag[] arrayOfProductTag = new ProductTag[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            ProductTag localProductTag = findById(paramArrayOfLong[i]);
            localProductTag.setName(paramArrayOfString[i]);
            arrayOfProductTag[i] = localProductTag;
            i++;
        }
        return arrayOfProductTag;
    }

    public ProductTag[] deleteByIds(Long[] paramArrayOfLong) {
        ProductTag[] arrayOfProductTag = new ProductTag[paramArrayOfLong.length];
        int i = 0;
        int j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfProductTag[i] = findById(paramArrayOfLong[i]);
            i++;
        }
        this.productMng.deleteTagAssociation(arrayOfProductTag);
        i = 0;
        j = paramArrayOfLong.length;
        while (i < j) {
            arrayOfProductTag[i] = this.productTagDao.deleteById(paramArrayOfLong[i]);
            i++;
        }
        return arrayOfProductTag;
    }

    @Autowired
    public void setProductMng(ProductMng paramProductMng) {
        this.productMng = paramProductMng;
    }

    @Autowired
    public void setDao(ProductTagDao paramProductTagDao) {
        this.productTagDao = paramProductTagDao;
    }
}

